/**
 * Project: lionbule-other
 * 
 * File Created at 2012-6-17
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package agds;

/**
 * TestThreadStatus.
 * 
 * @author LionBule 2012-6-17 下午5:20:35
 */
public class TestThreadStatus {
    static Object lock    = new Object();
    static Object lockNum = new Object();
    static int    num     = 0;

    /**
     * @param args
     */
    public static void main(String[] args) {
        testSleep();
        testBlock();
    }

    static void testSleep() {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                int i = 0;
                boolean flag = true;
                while (true) {
                    try {
                        synchronized (lock) {
                            Thread.sleep(2000);
                            System.out.println("sleep time=" + (i++));
                        }
                        synchronized (lockNum) {
                            if (flag) {
                                testWait_01();
                                testWait_02();
                                flag = false;
                            } else {
                                lockNum.notify();
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        });

        t.setName("TestSleep");
        t.start();
    }

    static void testBlock() {
        Thread t = new Thread(new Runnable() {
            int i = 0;

            @Override
            public void run() {
                while (true) {
                    synchronized (lock) {
                        System.out.println("testBlock time = " + (i++));
                    }
                }
            }

        });
        t.setName("testBlock");
        t.start();
    }

    static void testWait_01() {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    synchronized (lockNum) {
                        if (num % 2 == 0) {
                            try {
                                System.out.println("testWait_01 time = " + (num++));
                                lockNum.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }

        });
        t.setName("testWait_01");
        t.start();
    }

    static void testWait_02() {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    synchronized (lockNum) {
                        if (num % 2 == 1) {
                            try {
                                System.out.println("testWait_02 time = " + (num++));
                                lockNum.wait(3000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }

        });
        t.setName("testWait_02");
        t.start();
    }
}
